#coding:utf8
from config import *
from routin_base import *

mouse_p_gap = 0.1 #超过此时间间隔，认为鼠标停止，重置起点
mouse_p_dis = round(200*reso_sca) #到起点此距离内，鼠标不跳动
if is_working:
    mouse_p_dis = 250
mouse_p_speed = 3 #小于此速度，鼠标不跳动，不重置起点
mouse_p_speed_low = round(300*reso_sca) #小于此速度，重置起点
mouse_sqeed_interval = 0.1 #速度队列统计时长
mouse_sqeed_interval2 = 0.2 #速度队列2统计时长
mouse_p_dis_low = round(50*reso_sca) #速度队列2距离和小于此，重置起点

def is_solo(t):    
    return cfg.last_move_is_right_solo and t-cfg.left_mouse_time>0.2 and cfg.jump_state==0 and \
    cfg.jump_kuai==0 and not is_acc_up()

g_lastJ = False
def jump_bg_thr():
    global g_lastJ    
    while True:        
        time.sleep(0.001)
        tt = time.time()
        if is_jump_base() or not  cfg.mouse_tip_no_show:
            continue
        if not cfg.mouse_p_enable or not is_solo(tt):
            continue        
        lastj = is_mouse_p(tt)

        if g_lastJ and not lastj and tt-cfg.left_mouse_time>0.1:
            if len(cfg.stop_jump)==1:
                cfg.stop_jump = [cal_random(0),cal_random(0),0]
            if len(cfg.stop_jump)==2:
                cfg.stop_jump = [cal_random(0),cal_random(0)]
                dj = cfg.stop_jump
            elif len(cfg.stop_jump)==3:            
                dj = cfg.stop_jump[:2]                        
            cfg.supress = True
            time.sleep(0.002)            
            dx,dy = dj[0]
            aa = random.randint(1,98)
            gg = 1
            if aa>=cfg.jump_thre:
                gg = 2.5        
            if cfg.tt_co>right_jump_thre:
                mouse.Controller().move(round(gg*dx),round(gg*dy))
                print('bkmv')
            cfg.j_p=get_cur_pos()            
            time.sleep(0.002)            
            cfg.supress = False                        
        g_lastJ = lastj

def period_call():
    if time.time()-cfg.left_mouse_time>mouse_p_gap and time.time()-cfg.right1_mouse_time>mouse_p_gap:
        cfg.mouse_p_on = False
    elif not cfg.mouse_p_on:
        cfg.mouse_p_on = True
        cfg.mouse_p_last_pos = get_cur_pos()        

def is_mouse_p(t):    
    if not cfg.mouse_p_enable or not is_solo(t):
        return False
    if cfg.w2showing:        
        return False
    if not cfg.mouse_p_on:            
        return False    
    dd = cal_dis(get_cur_pos(),cfg.mouse_p_last_pos)    
    return dd>mouse_p_dis

def can_jump():
    if cfg.isShow or cfg.show_one_on or cfg.w2showing or cfg.min_ge!=0:
        return False
    return True

def do_check_jump(t):    
    if not can_jump():
        return False
    if is_mouse_p(t) and t- cfg.right_mouse_time<0.05 and cfg.mouse_speed_bk>mouse_p_speed:
        return True
    return  False

def do_suppress():
    return False
    if is_jump_base() or not  cfg.mouse_tip_no_show:
        return False
    
    t =time.time() 
    if not cfg.mouse_p_enable or not is_solo(t):
        return False
    
    if cfg.supress:        
        return True
    
    
    if t-cfg.last_ig_time>0.08:
        cfg.last_ig_time = t
    if t-cfg.last_ig_time<0.01 and do_check_jump(t):                        
        return True
    if t-cfg.right1_mouse_time<0.05 and t-cfg.last_ig_time<0.01 and can_jump() and not cfg.force_show:                        
        return True
    return  False

def cal_random(co):
    l = [[1, 1], [1, -1], [-1, 1], [-1, -1]]
    x, y = l[co%4]
    d = 10
    x = d * x
    y = d * y
    a = random.randint(0, 9999)
    t = 2 * math.pi * a / 10000
    dx, dy = math.cos(t), math.sin(t)    
    g = random.randint(jump_fan[0], jump_fan[1])
    return dx * g, dy * g

bgr_co = 0
bgr_time=0
def jump_bgr_2(t):    
    return
    global bgr_co,bgr_time
    if t-bgr_time<0.05:
        return
    bgr_time = t
    
    if do_check_jump(t):                           
        bgr_co += 1
        dx, dy = cal_random(bgr_co)            
        mouse.Controller().move(round(dx), round(dy))
        print('bkmv')

def for_mouse_jump(h,t,cpos):        
    if h in cfg.left_mouse:    
        cfg.mouse_p_last_pos = cpos
        
    if h in cfg.right_mouse:        
        if h==cfg.right_mouse[0]:
            cfg.right1_mouse_time = t

mosue_speed_q = deque()
def cal_mouse_speedbk(x,y,t):
    mosue_speed_q.append((x,y,t))
    while len(mosue_speed_q)>0:
        if t-mosue_speed_q[0][-1]>mouse_sqeed_interval:
            mosue_speed_q.popleft()
            continue
        else:
            break
    if len(mosue_speed_q)<2:
        cfg.mouse_speed_bk=0
        return    
    to = 0
    tx = 0
    ty = 0
    for i in mosue_speed_q:
        tx += i[0]
        ty += i[1]
    to = math.sqrt(tx*tx+ty*ty)
    cfg.mouse_speed_bk=to/mouse_sqeed_interval        

mosue_speed_q2 = deque()
def cal_mouse_speed2(x,y,t):    
    mosue_speed_q2.append((x,y,t))
    while len(mosue_speed_q2)>0:
        if t-mosue_speed_q2[0][-1]>mouse_sqeed_interval2:
            mosue_speed_q2.popleft()
            continue
        else:
            break    
    to = 0
    tx = 0
    ty = 0
    for i in mosue_speed_q2:
        tx += i[0]
        ty += i[1]
    to = math.sqrt(tx*tx+ty*ty)
    if to<mouse_p_dis_low:
        cfg.mouse_speed_bk=10        
        
def mv_call(h,t,cpos,x,y):
    if not cfg.mouse_p_enable or not is_solo(t):
        return
    if is_jump_base() or not  cfg.mouse_tip_no_show:
        return
    
    for_mouse_jump(h,t,cpos)
    cal_mouse_speedbk(x,y,t)                
    cal_mouse_speed2(x,y,t)                
    if cfg.mouse_speed_bk<mouse_p_speed_low:
        cfg.mouse_p_last_pos = cpos
        
    jump_bgr_2(t)
